using Autofac.Core;
using AutoMapper;
using CoreProject.DTO;
using CoreProject.IService;
using CoreProject.Model;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace CoreProject.Api.Controllers
{
  [Route("api/[controller]")]
  [ApiController]
  public class MenusController : ControllerBase
  {
    private readonly IMenusService _service;
    private readonly IMapper _mapper;
        private readonly IAuthorityService _authority;

        public MenusController(IMenusService service,IMapper mapper,IAuthorityService authority)
    {
      _service = service;
      _mapper = mapper;
            this._authority = authority;
        }

    // GET: api/<MenusController>
    [HttpGet]
    public async Task<ResultData<List<MenuDto>>> Get()
    {
      //JWT Token 解密后的 RoleId
      var role = HttpContext.User.FindFirst("RoleID");
      //从jwt 里面去 获取用户登录的 roleID
      ResultData<List<MenuDto>> result = new ResultData<List<MenuDto>>();


      if (role == null)
      {
        result.code = "-1";
        result.state = 200;
        result.msg = "无权的访问！";
        return result;
      }
      var roleID = int.Parse(role.Value);

      var menus = await _service.GetMenus(roleID);
      var list = _mapper.Map<List<MenuDto>>(menus);

      result.code = "1";
      result.msg = "ok";
      result.state = 200;
      result.data = list;

      return result;
    }
        [HttpGet("sy")]
        public async Task<ResultData<List<MenuDto>>> Getsy()
        {
            var data = await _service.GetListAsync(true, r => r.MenuId, r => true);
            var list = _mapper.Map<List<MenuDto>>(data);
            ResultData<List<MenuDto>> result = new ResultData<List<MenuDto>>();
            result.code = "1";
            result.msg = "ok";
            result.state = 200;
            result.data = list;
            return result;
        }

        // GET api/<MenusController>/5
        [HttpGet("page")]
        public async Task<ResultPageList<MenuDto>> GetListByPage(int page, int size, string name = "")
        {

            var data = await _service.GetMenusBypage(name, page, size);

            //手动变自动
            var list = _mapper.Map<List<MenuDto>>(data.list);

            ResultPageList<MenuDto> result = new ResultPageList<MenuDto>();
            result.state = 200;
            result.code = "1";
            result.msg = "ok";
            result.page = page;
            result.size = size;
            result.count = data.count;
            result.data = list;
            return result;
        }

        // POST api/<MenusController>
        [HttpPost]
    public async Task<ResultHandle> Post([FromBody] MenuDto menuDto )
    {
            var data= _mapper.Map<Menu>(menuDto);
            var bol =await _service.AddAsync(data);
            ResultHandle result = new ResultHandle();
            result.state = 200;
            result.code =bol ? "1" : "-1";
            result.msg = bol ? "添加成功!" : "添加失败!";
            return result;

        }

    // PUT api/<MenusController>/5
    [HttpPut]
    public async Task<ResultHandle> Put([FromBody] MenuDto menuDto)
    {

            var data = _mapper.Map<Menu>(menuDto);
            var bol = await _service.UpdateAsync(data);
            ResultHandle result = new ResultHandle();
            result.state = 200;
            result.code = bol ? "1" : "-1";
            result.msg = bol ? "修改成功!" : "修改失败!";
            return result;

        }

        // DELETE api/<MenusController>/5
        [HttpDelete]
    public async Task<ResultHandle> Delete(int id)
    {
          
          
            var bol = await _service.DeleteMenu(id);
            ResultHandle result = new ResultHandle();
            result.state = 200;
            result.code = bol ? "1" : "-1";
            result.msg = bol ? "删除成功!" : "删除失败!";
            return result;
        }

        [HttpDelete("DeleteBatch")]
        public async Task<ResultHandle> DeleteBatch([FromBody] int[] ids)
        {
          
            var bol = await _service.DeleteMenus(ids);
            ResultHandle result = new ResultHandle();
            result.state = 200;
            result.code = bol ? "1" : "-1";
            result.msg = bol ? "删除成功!" : "删除失败!";
            return result;
        }

    }
}
  
